package com.example.administrator.u17.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import com.example.administrator.u17.R;
import com.example.administrator.u17.adapter.SearchTopDetailAdapter;
import com.example.administrator.u17.adapter.SearchTopDetailViewPagerAdapter;
import com.example.administrator.u17.fragment.HotFragment;

import java.util.ArrayList;
import java.util.List;

public class SearchTopListDetailActivity extends AppCompatActivity {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<Fragment> mFragments;
    private ArrayList<String> mTitles;
    private SearchTopDetailViewPagerAdapter mAdapter;
    private ImageView mImageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_search_top_list_detail);
        initView();
        initData();
        setData();
        setListener();
    }

    private void setListener() {
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                for (int i = 0; i < mTabLayout.getTabCount(); i++) {
                    if (mTabLayout.getTabAt(i)==tab){
                       FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
                        mViewPager.setCurrentItem(i);
                        transaction.commit();
                        break;
                    }
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void setData() {
        mViewPager.setAdapter(mAdapter);
        //设置TabLayout与ViewPager的联动
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setSelectedTabIndicatorHeight(5);
        mTabLayout.setSelectedTabIndicatorColor(Color.GREEN);
        //设置标题的正常颜色和选择颜色
        mTabLayout.setTabTextColors(Color.GRAY,Color.GREEN);


    }

    private void initData() {
        mFragments=new ArrayList<>();
        mTitles=new ArrayList<>();
        Intent intent=getIntent();
        String title=intent.getStringExtra("title");

        String argName1=intent.getStringExtra("argName");
        Log.d("TAG", "initData: "+intent.getStringExtra("tabTitle"));
        String tabTitle1=intent.getStringExtra("tabTitle");
        int argValue1=intent.getIntExtra("argValue",0);
        int argCon1=intent.getIntExtra("argCon",0);

        String argName2=intent.getStringExtra("argName2");
        String tabTitle2=intent.getStringExtra("tabTitle2");
        int argValue2=intent.getIntExtra("argValue2",0);
        int argCon2=intent.getIntExtra("argCon2",0);
        mTitles.add(tabTitle1);
        mTitles.add(tabTitle2);
        for (int i = 0; i < mTitles.size(); i++) {
            HotFragment hotFragment=new HotFragment();
            Bundle bundle=new Bundle();
            bundle.putString("title",title);
            if (i==0) {
                bundle.putString("argName", argName1);
                bundle.putString("tabTitle", tabTitle1);
                bundle.putInt("argValue", argValue1);
                bundle.putInt("argCon", argCon1);

            }else if (i==1){
                bundle.putString("argName", argName2);
                bundle.putString("tabTitle", tabTitle2);
                bundle.putInt("argValue", argValue2);
                bundle.putInt("argCon", argCon2);
            }
            hotFragment.setArguments(bundle);
            mFragments.add(hotFragment);
        }

        mAdapter=new SearchTopDetailViewPagerAdapter(getSupportFragmentManager(),mFragments,mTitles);
    }

    private void initView() {
        mTabLayout= (TabLayout) findViewById(R.id.tl_search_top_detail_activity);
        mViewPager= (ViewPager) findViewById(R.id.vp_search_top_detail_activity);
        mImageView= (ImageView) findViewById(R.id.iv_back_search_detail_activity);
    }
}
